package com.laopan.elasticsearch.core.operation;

import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;

/**
 * 索引的实际操作者
 *
 * @author laopan
 * @className EsIndexServiceImpl
 * @date 2021/4/16 15:31
 */

public class IndexOperation {
    private static final Logger log = LoggerFactory.getLogger(IndexOperation.class);

    private RestHighLevelClient highLevelClient;

    public IndexOperation(RestHighLevelClient highLevelClient) {
        this.highLevelClient = highLevelClient;
    }

    public boolean deleteIndex(String index) {
        DeleteIndexRequest request = new DeleteIndexRequest(index);
        try {
            AcknowledgedResponse delete = highLevelClient.indices().delete(request, RequestOptions.DEFAULT);
            if (delete.isAcknowledged()) {
                log.info("delete elasticsearch index:[{}] success", index);
                return true;
            } else {
                log.info("delete elasticsearch index:[{}] fail", index);
            }
        } catch (Exception e) {
            log.error("delete elasticsearch index:[{}] , exception;{} ", index, e);
        }
        return false;
    }

    public boolean createIndex(String index, XContentBuilder xContentBuilder) {
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
            createIndexRequest.mapping(xContentBuilder);
            CreateIndexResponse createIndexResponse = highLevelClient.indices().create(createIndexRequest,
                    RequestOptions.DEFAULT);
            if (createIndexResponse.isAcknowledged()) {
                log.info("创建索引成功");
                return true;
            }
        } catch (IOException e) {
            log.error("创建索引异常", e);
        }
        return false;
    }

    public boolean createIndex(String index, String json) {
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
            createIndexRequest.mapping(json, XContentType.JSON);
            CreateIndexResponse createIndexResponse = highLevelClient.indices().create(createIndexRequest,
                    RequestOptions.DEFAULT);
            if (createIndexResponse.isAcknowledged()) {
                log.info("创建索引成功");
                return true;
            }
        } catch (IOException e) {
            log.error("创建索引异常", e);
        }
        return false;
    }

    public boolean isExist(String index) {
        GetIndexRequest indexRequest = new GetIndexRequest(index);
        try {
            return highLevelClient.indices().exists(indexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            log.error("get elasticsearch index:[{}] isExists exception;{} ", index, e);
        }
        return false;
    }
}
